from typing import List

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums=sorted(nums)
        ans=[]

        for i in range(len(nums)):
            if nums[i]>0:
                return ans

            if i>0 and nums[i-1]==nums[i]:
                continue
            
            left=i+1
            right=len(nums)-1
            while left<right:
                sums=nums[i]+nums[left]+nums[right]
                if sums>0:
                    right-=1
                elif sums<0:
                    left+=1
                else:#[0,0,0,0]
                    ans.append([nums[i],nums[left],nums[right]])
                    while left<right and nums[left]==nums[left+1]:
                        left+=1
                    while left<right and nums[right]==nums[right-1]:
                        right-=1

                    left+=1
                    right-=1

        return ans
